package cn.fintecher.pangolin.service.business.web;

import cn.fintecher.pangolin.common.enums.HistoryCaseStatus;
import cn.fintecher.pangolin.common.model.UserModel;
import cn.fintecher.pangolin.entity.elastic.HistoryCases;
import cn.fintecher.pangolin.service.business.model.request.AddWorkOrderRequest;
import cn.fintecher.pangolin.service.business.model.request.CaseToNormalRequest;
import cn.fintecher.pangolin.service.business.model.request.DeleteReturnCasesRequest;
import cn.fintecher.pangolin.service.business.model.request.ReturnCaseQueryRequest;
import cn.fintecher.pangolin.service.business.model.response.ReturnCasesResponse;
import cn.fintecher.pangolin.service.business.repository.elasticsearch.HistoryCasesRespository;
import cn.fintecher.pangolin.service.business.service.BaseService;
import cn.fintecher.pangolin.service.business.service.HistoryCasesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery;

/**
 * @Author wangxiangdong
 * @Description personal操作相关
 * @Date 2019/9/10 10:08
 **/
@RestController
@RequestMapping("/api/historyCasesController")
@Api(value = "personal操作相关", description = "personal操作相关")
public class HistoryCasesController {

    @Autowired
    private HistoryCasesService historyCasesService;

    @Autowired
    private HistoryCasesRespository historyCasesRespository;

    @Autowired
    private ModelMapper modelMapper;

    @Autowired
    private BaseService baseService;

    @GetMapping("/queryReturnCases")
    @ApiOperation(value = "查询退案案件", notes = "查询退案案件")
    public ResponseEntity<Page<ReturnCasesResponse>> queryReturnCases(Pageable pageable, ReturnCaseQueryRequest request, @RequestHeader(value = "X-UserToken") String token) {
        UserModel userModel = baseService.getUserByToken(token);
        BoolQueryBuilder qb = request.generateQueryBuilder();
        baseService.spliceBuilder(qb, userModel);
        qb.must(matchPhraseQuery("historyCaseStatus.keyword", HistoryCaseStatus.RETIRE_POOL.name()));
        SortBuilder sortBuilder = SortBuilders.fieldSort("endCaseDate").unmappedType("date").order(SortOrder.DESC);
        SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(qb).withPageable(pageable).withSort(sortBuilder).build();
        Page<HistoryCases> historyCasesPage = historyCasesRespository.search(searchQuery);
        Page<ReturnCasesResponse> responses = historyCasesPage.map(personal -> modelMapper.map(personal, ReturnCasesResponse.class));
        return ResponseEntity.ok().body(responses);
    }

    @GetMapping("/queryDeleteCases")
    @ApiOperation(value = "删除案件查询）", notes = "删除案件查询")
    public ResponseEntity<Page<ReturnCasesResponse>> getDeleteCaseForCaseManage(Pageable pageable, ReturnCaseQueryRequest request, @RequestHeader(value = "X-UserToken") String token) {
        UserModel userModel = baseService.getUserByToken(token);
        BoolQueryBuilder qb = request.generateQueryBuilder();
        baseService.spliceBuilder(qb, userModel);
        qb.must(matchPhraseQuery("historyCaseStatus.keyword", HistoryCaseStatus.DELETE_POOL.name()));
        Sort sort = new Sort(Sort.Direction.DESC, "deleteCaseDate");
        pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), sort);
        Page<HistoryCases> hisCases = historyCasesRespository.search(qb, pageable);
        Page<ReturnCasesResponse> responses = hisCases.map(personal -> modelMapper.map(personal, ReturnCasesResponse.class));
        return ResponseEntity.ok().body(responses);
    }

    @GetMapping("/deleteCases")
    @ApiOperation(value = "退案案件删除", notes = "退案案件删除")
    public ResponseEntity<Void> deleteCases(DeleteReturnCasesRequest deleteReturnCasesRequest) {
        historyCasesService.deleteCases(deleteReturnCasesRequest);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/getReturnCasesBatchNumber")
    @ApiOperation(value = "获取退案案件的批次号", notes = "获取退案案件的批次号")
    public ResponseEntity<List<String>> getReturnCasesBatchNumber(@RequestHeader(value = "X-UserToken") String token) {
        UserModel userModel = baseService.getUserByToken(token);
        List<String> batchNumberList = historyCasesService.getReturnCasesBatchNumber(userModel);
        return ResponseEntity.ok().body(batchNumberList);
    }

    @GetMapping("/getDeleteCasesBatchNumber")
    @ApiOperation(value = "获取删除案件的批次号", notes = "获取删除案件的批次号")
    public ResponseEntity<List<String>> getDeleteCasesBatchNumber(@RequestHeader(value = "X-UserToken") String token) {
        UserModel userModel = baseService.getUserByToken(token);
        List<String> batchNumberList = historyCasesService.getDeleteCasesBatchNumber(userModel);
        return ResponseEntity.ok().body(batchNumberList);
    }

    @GetMapping("/deleteCasesPermanent")
    @ApiOperation(value = "永久删除案件信息", notes = "永久删除案件信息")
    public ResponseEntity<Void> deleteCasesPermanent(DeleteReturnCasesRequest deleteReturnCasesRequest) {
        historyCasesService.deleteCasesPermanent(deleteReturnCasesRequest);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/recoverCaseToNormal")
    @ApiOperation(value = "退案恢复", notes = "退案恢复")
    public ResponseEntity<Void> recoverCaseToNormal(@RequestBody CaseToNormalRequest caseToNormalRequest, @RequestHeader(value = "X-UserToken") String token) {
        UserModel userByToken = baseService.getUserByToken(token);
        historyCasesService.recoverCaseToNormal(caseToNormalRequest, userByToken.getRealName());
        return ResponseEntity.ok().build();
    }

    @ApiOperation(value = "添加退案工单", notes = "添加退案工单")
    @PostMapping("/addHistoryCaseWorkerOrder")
    public ResponseEntity<Void> addHistoryCaseWorkerOrder(@Valid @RequestBody AddWorkOrderRequest request, @RequestHeader(value = "X-UserToken") String token) {
        UserModel userModel = baseService.getUserByToken(token);
        historyCasesService.addHistoryCaseWorkerOrder(request, userModel);
        return ResponseEntity.ok().build();
    }
}
